package gavdi.dk.data;

import java.util.ArrayList;
import java.util.List;

import gavdi.dk.model.Absence;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.SyncStateContract.Columns;

public class DAOAbsence {

	  // Database fields
	  private SQLiteDatabase database;
	  private MySQLiteHelper dbHelper;
	  private String[] allColumns = {
	      MySQLiteHelper.COLUMN_NAME,
	      MySQLiteHelper.COLUMN_CODE};
	  
	  public DAOAbsence(Context context) {
	    dbHelper = new MySQLiteHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public String createAbsence(Absence abs) {
	    open();
		ContentValues values = new ContentValues();

	    values.put(MySQLiteHelper.COLUMN_NAME, abs.getName());
	    values.put(MySQLiteHelper.COLUMN_CODE, abs.getCode());

	    long res = database.insert(MySQLiteHelper.TABLE_ABSENCE, null,
	        values);
	    close();
	    return Long.toString(res);
	  }

	  public boolean deleteAbsence(Absence absence) {
	    open();
	    int count = database.delete(MySQLiteHelper.TABLE_ABSENCE, MySQLiteHelper.COLUMN_ID
	        + " = " + absence.getId(), null);
	    close();
	    if(count == 0)
	    	return false;
	    return true;
	  }

	  public Absence getAbsenceByCode(String code) {
		open();
	    Cursor cursor = database.query(MySQLiteHelper.TABLE_ABSENCE,
	        allColumns, MySQLiteHelper.COLUMN_CODE + "="+"\""+code+"\"", null, null, null, null);
	    if(!cursor.moveToFirst())
	    	return null;
	    Absence abs = cursorToAbsence(cursor);
	      // Make sure to close the cursor
	    cursor.close();
	    close();  
	  
	    return abs;

	    
	  }
	  public ArrayList<Absence> getAllAbsence() {
		  open();  
		  ArrayList<Absence> absList = new ArrayList<Absence>();

		  Cursor cursor = database.query(MySQLiteHelper.TABLE_ABSENCE,
		        allColumns, null, null, null, null, null);

		  if(!cursor.moveToFirst())
		  	return null;
		  while (!cursor.isAfterLast()) {
			  Absence abs;
		    		 abs = cursorToAbsence(cursor);
		    		 absList.add(abs);
		    		 cursor.moveToNext();
		  }
		    // Make sure to close the cursor
		  cursor.close();
		  close();
		  return absList;
		  }
	  public boolean exists(String code){
		  boolean res = false;
		  open();
		  Cursor cursor = 	database.query(MySQLiteHelper.TABLE_ABSENCE,
			        		new String[]{MySQLiteHelper.COLUMN_ID}, 
			        		MySQLiteHelper.COLUMN_CODE+"="+"\""+code+"\"", null, null, null, null);
		  res = cursor.moveToFirst();
		  cursor.close();
		  close();
		  return res;
	  }
	  
	  
	  private Absence cursorToAbsence(Cursor cursor) {
		  Absence abs = new Absence();
		  abs.setName(cursor.getString(0));
		  abs.setCode(cursor.getString(1));

	    return abs;
	  }

}
